package com.projectName.www.servlet;


import com.projectName.www.service.OpenBuyer;
import com.projectName.www.service.Y_J_buycar;
import com.projectName.www.service.Y_J_intoshop;
import com.projectName.www.service.Y_J_orders;
import com.projectName.www.dao.Y_J_find_dao;
import com.projectName.www.dao.Y_J_intoshop_dao;
import com.projectName.www.dao.Y_J_moneycount_dao;
import com.projectName.www.dao.Y_J_select_dao;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.util.List;
import java.util.Vector;

/**
     * @author 86132
     *
     */
    public class Y_J extends JFrame {

        private JPanel contentPane;
        private static JTable select_table;
        private JTextField findname;
        private static JTextField user_money;
        private JTable selectmessage_table;
              public static  String findingname;


        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Y_J frame = new Y_J();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        /**
         * Create the frame.
         */
        public Y_J() throws Exception {
            setTitle("欢迎");
           // setAlwaysOnTop(true);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(3);
            setBounds(100, 100, 1067, 878);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

            setContentPane(contentPane);

            JDesktopPane desktopPane = new JDesktopPane();
            desktopPane.setBackground(SystemColor.inactiveCaptionBorder);
            GroupLayout gl_contentPane = new GroupLayout(contentPane);
            gl_contentPane.setHorizontalGroup(
                    gl_contentPane.createParallelGroup(Alignment.LEADING)
                            .addGroup(gl_contentPane.createSequentialGroup()
                                    .addContainerGap()
                                    .addComponent(desktopPane, GroupLayout.PREFERRED_SIZE, 1046, GroupLayout.PREFERRED_SIZE)
                                    .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            gl_contentPane.setVerticalGroup(
                    gl_contentPane.createParallelGroup(Alignment.LEADING)
                            .addGroup(gl_contentPane.createSequentialGroup()
                                    .addContainerGap()
                                    .addComponent(desktopPane, GroupLayout.PREFERRED_SIZE, 770, GroupLayout.PREFERRED_SIZE)
                                    .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );

            JScrollPane scrollPane = new JScrollPane();
            scrollPane.setBounds(10, 102, 406, 700);
            desktopPane.add(scrollPane);

            select_table = new JTable();
            select_table.setFont(new Font("宋体", Font.PLAIN, 23));
            select_table.setSize(80,60);
            select_table.setModel(new DefaultTableModel(
                    new Object[][] {
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                            {null, null, null},
                    },
                    new String[] {
                            "\u5E97\u94FA", "\u83DC\u54C1", "\u63CF\u8FF0"
                    }

            ));
            select_table.getColumnModel().getColumn(0).setPreferredWidth(65);
            select_table.getColumnModel().getColumn(1).setPreferredWidth(65);
            scrollPane.setViewportView(select_table);

            JLabel lblNewLabel = new JLabel("输入店铺或者菜名：");
            lblNewLabel.setFont(new Font("宋体", Font.BOLD, 17));
            lblNewLabel.setBounds(10, 33, 166, 40);
            desktopPane.add(lblNewLabel);

            findname = new JTextField();
            findname.setBounds(167, 31, 171, 48);
            desktopPane.add(findname);
            findname.setColumns(10);

            JLabel lblNewLabel_1 = new JLabel("账户：");
            lblNewLabel_1.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 45));
            lblNewLabel_1.setBounds(426, 33, 140, 74);
            desktopPane.add(lblNewLabel_1);

            user_money = new JTextField();
            user_money.setEditable(true);
            user_money.setBounds(559, 61, 95, 38);
            desktopPane.add(user_money);
            user_money.setColumns(10);

            JButton  orders = new JButton("我的订单");
            orders.addActionListener(e->
            {
                Y_J_orders yj= null;
                try {
                    yj = new Y_J_orders();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                yj.setVisible(true);
                desktopPane.add(yj);
            });
            orders.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 25));
            orders.setBounds(708, 148, 182, 84);
            desktopPane.add(orders);

            JButton buycar = new JButton("我的购物车");
            buycar.addActionListener(e-> {


                        Y_J_buycar yj = null;
                        try {
                            yj = new Y_J_buycar();
                        } catch (Exception ex) {
                            throw new RuntimeException(ex);
                        }
                        yj.setVisible(true);
                desktopPane.add(yj);
            }

            );
            buycar.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 25));
            buycar.setBounds(453, 148, 182, 84);
            desktopPane.add(buycar);

            JButton insertmoney = new JButton("充值");
            insertmoney.addActionListener(e->
            {
                try {
                    addmoney(e);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            });
            insertmoney.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 25));
            insertmoney.setBounds(708, 43, 182, 84);
            desktopPane.add(insertmoney);

            JButton find = new JButton("查询");
            find.addActionListener(e->
            {
                try {
                    find(e);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            });
            find.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 25));
            find.setBounds(453, 262, 182, 84);
            desktopPane.add(find);

            JButton intoshop = new JButton("进入店铺");
            intoshop.addActionListener(e->
            {

                JOptionPane.showMessageDialog(null,"查询后才能进入店铺");
                  Y_J_intoshop yj = null;
                try {
                    yj = new Y_J_intoshop();
                    yj.setTitle("当前店铺："+findingname);

                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }

                yj.setVisible(true);
                desktopPane.add(yj);
            });
            intoshop.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 25));
            intoshop.setBounds(708, 262, 182, 84);
            desktopPane.add(intoshop);

            JScrollPane scrollPane_1 = new JScrollPane();
            scrollPane_1.setBounds(453, 374, 484, 161);
            desktopPane.add(scrollPane_1);

            selectmessage_table = new JTable();
            selectmessage_table.setModel(new DefaultTableModel(
                    new Object[][] {
                            {null},
                            {null},
                            {null},
                            {null},
                            {null},
                            {null},
                            {null},
                            {null},
                            {null},
                            {null},
                    },
                    new String[] {
                            "\u5E97\u94FA\u7684\u8BC4\u4EF7\u7559\u8A00"
                    }
            ));
            selectmessage_table.getColumnModel().getColumn(0).setPreferredWidth(293);
            scrollPane_1.setViewportView(selectmessage_table);
            contentPane.setLayout(gl_contentPane);
            fillmoney();
           fillselecttable();

        }

    private void find(ActionEvent e) throws Exception {
            ResultSet r=Y_J_select_dao.filltable();
        if(findname.getText().equals("") )

        {
            JOptionPane.showMessageDialog(null,"查询对象不能为空");
            return;
        }
        List <Vector> v= Y_J_find_dao.fill(r,findname.getText());
        if(v.size()==0){
            JOptionPane.showMessageDialog(null,"没有该查询对象");
            return;
        }
        DefaultTableModel  model = (DefaultTableModel)select_table.getModel();
        model.setRowCount(0);
       findingname= (String) v.get(0).get(0);
        for (int i = 0; i < v.size(); i++) {
            model.addRow(v.get(i));
        }

        int rowCount = model.getRowCount();
        for(int i=0;i<32-rowCount;i++)
        {
             Vector V=new Vector();
            V.add("");
            model.addRow(V);
        }
        DefaultTableModel  m = (DefaultTableModel)selectmessage_table.getModel();
        m.setRowCount(0);
        String  name= (String) model.getValueAt(1,1);
        ResultSet resultSet = Y_J_find_dao.resultSet();
        while (resultSet.next())
        {
            Vector v1=new Vector();
            if(resultSet.getString(1)==null)
            {
                v1.add("商家的留言："+resultSet.getString(3));
            }
            else v1.add(resultSet.getString(1)+"的评价："+resultSet.getString(3));
            m.addRow(v1);
        }
    }
    //充值
    private void addmoney(ActionEvent e) throws Exception {
            if(Y_J_moneycount_dao.upmoney(OpenBuyer.username, Integer.parseInt(user_money.getText()))
                    &&Integer.parseInt(user_money.getText())>=0)
            {
                JOptionPane.showMessageDialog(null,"充值成功");

            }
            else JOptionPane.showMessageDialog(null,"充值失败");
        }

        public static  void fillmoney() throws Exception {
            if(Y_J_moneycount_dao.getmoney(OpenBuyer.username)<=0)
            {
                JOptionPane.showMessageDialog(null,"余额不足请充值");
            }
            user_money.setText(""+Y_J_moneycount_dao.getmoney(OpenBuyer.username));
        }

    //初始化查询店铺页面
    public static void fillselecttable() throws Exception {
        DefaultTableModel  model = (DefaultTableModel)select_table.getModel();
        model.setRowCount(0);
        ResultSet fill = Y_J_select_dao.filltable();

        while (fill .next())
        {
            Vector v=new Vector();
            v.add(fill.getString(1));
            v.add(fill.getString(2));
            v.add(fill.getString(3));
            model.addRow(v);
        }
        int rowCount = model.getRowCount();
        for(int i=0;i<32-rowCount;i++)
        {
            Vector v=new Vector();
          v.add("");
            model.addRow(v);
        }
    }
    //更新账户数值
    public static void upmoney() throws Exception {
        ResultSet upmoney = Y_J_intoshop_dao.upmoney(OpenBuyer.username);
        upmoney.next();
        user_money.setText(upmoney.getString("money"));
    }

}
